<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	<title></title>
	<script type="text/javascript" src="../../js/boot.js"></script>
</head>

<body>
	<div class="mini-splitter" style="width: 100%; height: 100%;">
		<div size="240" showCollapseButton="true">
			<div class="mini-fit">
				<div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
				</div>
				<ul id="pageTree" class="mini-tree" style="width: 100%;" showTreeIcon="true" allowDrag="true" allowDrop="true" textField="departName"
					idField="departID" resultAsTree="false">
				</ul>
			</div>
		</div>

		<div showCollapseButton="true">
			<fieldset id="fd1" style="width: 97%; margin-bottom: 10px;">
				<legend><span>查询条件</span></legend>
				<div id="queryForm" class="fieldset-body">
					<table class="form-table" border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td class="form-label">
								<span>姓名：</span>
								<div id="txtUserID" class="mini-autocomplete" style="width:150px;" popupWidth="200" textField="userName" valueField="userID">
									<div property="columns">
										<div header="姓名" field="userName" width="40"></div>
										<div header="编号" field="workNo"></div>
									</div>
								</div>
								<input id="comUserID" class="mini-combobox" textField="userName" valueField="userID" style="width: 150px">
								<span>日期从：</span>
								<input id="calendarDate_from" name="calendarDate_from" class="mini-datepicker" format="yyyy-MM-dd" style="width: 150px" />
								<span>到：</span>
								<input id="calendarDate_to" name="calendarDate_to" class="mini-datepicker" format="yyyy-MM-dd" style="width: 150px" />
								<a style="margin: 0 0 0 0;" class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
								<a style="margin: 0 0 0 0;" class="mini-button" iconCls="icon-add" onclick="openAdd()">新增</a>
								<a style="margin: 0 0 0 0;" class="mini-button" iconCls="icon-remove" onclick="doDelete()">删除</a>
								<a style="margin: 0 0 0 0;" class="mini-button" iconCls="icon-save" onclick="doSubmit()">提交</a>
								<a class="mini-menubutton " menu="#popupMenu">审核</a>
								<ul id="popupMenu" class="mini-menu" style="display:none;">
									<li iconCls="icon-ok" onclick="doCheck('pass')">通过</li>
									<li iconCls="icon-remove" onclick="doCheck('reject')">不通过</li>
								</ul>
								<a class="mini-menubutton " menu="#popupMenu2">设置</a>
								<ul id="popupMenu2" class="mini-menu" style="display:none;">
									<li iconCls="icon-search" onclick="userSet()">个性设置</li>
									<!-- <li iconCls="icon-ok" onclick="createReport()">生成日报</li> -->
								</ul>
							</td>
						</tr>
					</table>
				</div>
			</fieldset>

			<div class="mini-fit">
				<div id="datagrid" class="mini-datagrid" style="width:1700px;height:100%;" pageSize="20" allowResize="true" multiSelect="true">
					<div property="columns">
						<div name="checkName" type="checkcolumn"></div>
						<!--<div name="userID" width="0"></div>
							<div name="dayReportID" width="0"></div>
							<div name="projectDayID" field="dayWork.projectDayID" width="0"></div>-->
						<div name="userName" field="userName" headerAlign="center" width="40">用户姓名</div>
						<div name="departName" field="departName" headerAlign="center" width="50">部门名称</div>
						<div name="calendarDate" field="calendarDate" headerAlign="center" width="40">日期</div>
						<div name="checkStatus" field="checkStatus" headerAlign="center" width="40" renderer="onGridRenderer">审核状态</div>
						<div field="dayWork.packetName" headerAlign="center" width="100">工作包</div>
						<div field="dayWork.sortName" headerAlign="center" width="100">工时分项</div>
						<div field="dayWork.drawName" headerAlign="center" width="100">图纸名称</div>
						<div field="dayWork.workTime" headerAlign="center" width="50">工作时长</div>
						<div field="dayWork.hasWorkedRate" headerAlign="center" width="20" renderer="onWorkedRateRenderer">进度</div>
						<div name="overTime" field="overTime" headerAlign="center" width="30">加班时长</div>
						<div name="applyTime" field="applyTime" headerAlign="center" width="30">请假时长</div>
						<div name="totalWorkTime" field="totalWorkTime" headerAlign="center" width="30">工作总时</div>
						<div field="cardRecord" headerAlign="center" width="140">考勤记录</div>
						<!--<div name="action" headerAlign="center" width="60">操作</div>-->
					</div>
				</div>
			</div>
		</div>
	</div>

</body>

<script type="text/javascript">
	checkStatus = [
		{ id: 0, text: "未提交" },
		{ id: 1, text: "已提交" },
		{ id: 2, text: "已通过" }
	];

	miniInit();

	var userID = null;
	var localUser = mini.decode(localStorage.getItem('user'));
	if (localUser == null) {
		alert("你还没有登录，请先登录再操作...");
	}
	// 如果是普通用户，使用下拉选择控件，把本人及本人的手下加载进来
	else {
		userID = localUser.userID;
		if (localUser.roleID == "normalUser") {
			mini.get("txtUserID").setVisible(false);
			var comUserID = mini.get("comUserID");
			comUserID.setName("userID");
			http.get("/day/report/user/biz/list.do", function (page) {
				if (page.data.length > 0) {
					comUserID.setData(page.data);
					comUserID.select(0);
					comUserID.on("valuechanged", function (e) {
						// var user = e.selected;
						search();
					});
				}
			});
		}
		// 如果是平台管理员，使用自动补全控件
		else {
			mini.get("comUserID").setVisible(false);
			var txtUserID = mini.get("txtUserID");
			txtUserID.setName("userID");
			txtUserID.set({
				url: http.getUrl("/user/page.do")
			});
			txtUserID.setValue(localUser.userID);
			txtUserID.setText(localUser.userName);
		}
	}

	// 设置截止日期至今天
	var date = new Date();
	date.setDate(date.getDate() + 1);
	mini.get("calendarDate_to").setValue(date);
	date.setDate(date.getDate() - 4);
	mini.get("calendarDate_from").setValue(date);

	// 加载浅蓝色背景和去除滚动条
	$('.mini-splitter').addClass('bg');
	//$('.fieldset-body-body').addClass('bg');
	var tree = mini.get("pageTree");
	// tree.url = http.getUrl("/sys/org/list.do");
	// 如果是普通用户，则不加载组织结构树
	if (localUser.roleID != "normalUser") {
		// tree.load(tree.url);
		http.get('/sys/depart/list.do', function (page) {
			tree.loadData(page);
		});
	}
	var editNode = null; // 当前编辑的节点
	/*********************************************************
	 * 左侧目录树的鼠标点击事件
	 *********************************************************/
	tree.on("nodedblclick", function (e) {
		// debugger
		editNode = e.node;
		if (tree.getChildNodes(editNode).length > 0) {
			return;
		}
		http.get('/sys/depart/list.do?departID=' + editNode.departID, function (page) {
			tree.addNodes(page, editNode);
			tree.expandNode(editNode);
		});
		/* tree.loadNode(editNode);
		if (editNode) {
			// search();
		} */
	});

	/*********************************************************
	 * 右侧查询表单及数据表格
	 *********************************************************/
	var queryForm = new mini.Form("#queryForm");
	var datagrid = mini.get("datagrid"); // 表定义列表
	datagrid.set({
		url: http.getUrl("/day/report/detail/page.do")
	});

	datagrid.on("drawcell", function (e) {
		var record = e.record,
			column = e.column,
			field = e.field,
			value = e.value;
		var uid = record._uid;
		//action列，超连接操作按钮
		if (e.column.name == "action") {
			e.cellStyle = "text-align:center";
			if (record.userID == localUser.userID) {
				if (record.checkStatus != 1 && record.checkStatus != 2) {
					e.cellHtml = '<a href="javascript:editTable(\'' + uid + '\')">编辑</a>&nbsp; ';
				} else {
					e.cellHtml = '<a href="javascript:editTable(\'' + uid + '\')">查看</a>&nbsp; ';
				}
			} else {
				if (record.checkStatus == 1) {
					e.cellHtml = '<a href="javascript:editTable(\'' + uid + '\')">审核</a>&nbsp; ';
				} else {
					e.cellHtml = '<a href="javascript:editTable(\'' + uid + '\')">查看</a>&nbsp; ';
				}
			}
		}
	});
	datagrid.on("load", function () {
		// datagrid.mergeColumns(["userName", "departName", "calendarDate"]);
		// datagrid.mergeColumns(["userName", "departName", "calendarDate", "workTime", "overTime", "applyTime", "checkStatus"]);
		var list = datagrid.getData();
		if (list.length > 0) {
			var calendarDate = "";
			var rows = [];
			for (var i = 0; i < list.length; i++) {
				if (list[i].calendarDate != calendarDate) {
					rows.push(i);
					calendarDate = list[i].calendarDate;
				}
			}
			rows.push(list.length);
			/*var cells = [{
					rowIndex: 0,
					columnIndex: 3,
					rowSpan: 3,
					colSpan: 1
				}
			];
			datagrid.mergeCells(cells); */
			// debugger
			var cells = [];
			for (var i = 0; i < rows.length - 1; i++) {
				// 选择
				cells.push({
					rowIndex: rows[i],
					columnIndex: 0,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 用户姓名
				cells.push({
					rowIndex: rows[i],
					columnIndex: 1,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 部门名称
				cells.push({
					rowIndex: rows[i],
					columnIndex: 2,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 日期
				cells.push({
					rowIndex: rows[i],
					columnIndex: 3,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 审核状态
				cells.push({
					rowIndex: rows[i],
					columnIndex: 4,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 审核状态
				cells.push({
					rowIndex: rows[i],
					columnIndex: 4,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 工作时长
				cells.push({
					rowIndex: rows[i],
					columnIndex: 10,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 加班时长
				cells.push({
					rowIndex: rows[i],
					columnIndex: 11,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 请假时长
				cells.push({
					rowIndex: rows[i],
					columnIndex: 12,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
				// 考勤记录
				cells.push({
					rowIndex: rows[i],
					columnIndex: 13,
					rowSpan: rows[i + 1] - rows[i],
					colSpan: 1
				});
			}
			datagrid.mergeCells(cells);
		}
	});

	datagrid.on("rowdblclick", function (e) {
		var record = e.record,
			column = e.column,
			field = e.field,
			value = e.value;
		var uid = record._uid;
		var row = datagrid.getRowByUID(uid);
		editTable(row, "update");
	});

	// 打开编辑窗体
	function editTable(row, type) {
		var userID = row.userID;
		var dayReportID = row.dayReportID;
		var projectDayID = null;
		if (type == "update") {
			projectDayID = row.dayWork.projectDayID;
		}
		// debugger
		http.get('/user/get.do?userID=' + userID, function (page) {
			if (page.success) {
				// 如果类型是2，就是技术部的日报，使用复杂的页面打开
				var type = page.data.dayReportType;
				var html = "s.html";
				var height = 480;
				if (type == 2) {
					html = "f.html";
					height = 680;
				}
				mini.open({
					url: ctx + '/page/work/day_report_detail_' + html,
					title: "编辑",
					width: 880,
					height: height,
					onload: function () {
						var iframe = this.getIFrameEl();
						setTimeout(function () {
							iframe.contentWindow.setData(dayReportID, projectDayID);
						}, 300);
					},
					ondestroy: function (action) {
						/*if(action == "ok") {
							search();
						}*/
						search();
					}
				});
			}
		});
	}
	// 查询操作
	function search() {
		var data = queryForm.getData(true, false);
		var node = tree.getSelectedNode();
		if (node) {
			data["departID"] = node.departID;
			data["departID_link"] = "like";
		}
		// debugger
		// 如果角色是普通用户
		if (localUser.roleID == "normalUser") {
			data["condition"] = "userID='#userID#' or leaderID='#userID#'";
		}
		data["orderby"] = ["calendarDate,desc"];
		datagrid.load(data, null, miniui.showLoadErrorMessageBox);
	}
	setTimeout(function () {
		search();
	}, 3000);

	// 打开个性化设置窗体
	function userSet() {
		if (localUser == null && localUser.userID == null) {
			alert("你还没有登录");
			return;
		}
		var userID = localUser.userID;
		mini.open({
			url: ctx + '/page/work/day_report_detail_f_set.html',
			title: "编辑",
			width: 880,
			height: 680,
			onload: function () {
				var iframe = this.getIFrameEl();
				iframe.contentWindow.initial();
			},
			ondestroy: function (action) { }
		});
	}

	function openAdd() {
		var row = datagrid.getSelected();
		if (row) {
			editTable(row, "new");
		} else {
			mini.alert("请先选择要填写的日报记录", "提示", null);
		}
	}

	function openEdit() {
		var row = datagrid.getSelected();
		if (row) {
			editTable(row, "update");
		}
	}

	function doDelete() {
		var row = datagrid.getSelected();
		if (row) {
			debugger
			mini.confirm("确定要删除吗？", "确定？",
				function (action) {
					if (action == "ok") {
						// 状态为未提交，是自己的日报记录，才能删除
						if (row.checkStatus == 0 && row.userID == userID) {
							if (row.dayWork.projectDayID) {
								var data = {
									"id": row.dayWork.projectDayID
								};
								http.post("/day/report/deleteProjectDayWork.do", data, function (page) {
									if (page.success) {
										mini.alert("删除成功!", "提示", null);
										search();
									} else {
										mini.alert(page.message, "提示", null);
									}
								});
							} else {
								mini.alert("没有可删除的记录", "提示", null);
							}
						} else {
							mini.alert("你选择的记录不是自己的日报或者不是未提交状态", "提示", null);
						}
					}
				}
			);
		} else {
			mini.alert("请先选择要删除的日报记录", "提示", null);
		}
	}

	function doSubmit() {
		var rows = datagrid.getSelecteds();
		if (rows.length == 0) {
			mini.alert("请先选择要提交的记录", "提示", null);
			return;
		}
		var list = [];
		var flag = false;
		debugger
		for (var i = 0; i < rows.length; i++) {
			// 状态为未提交，工作时长或请假不为0，是自己的日报记录
			if (rows[i].checkStatus == 0 && (rows[i].workTime > 0 || rows[i].overTime > 0 || rows[i].applyTime > 0) && rows[i].userID == userID) {
				list.push(rows[i]);
				if (rows[i].workTime < 8) {
					flag = true;
				}
			}
		}
		if (list.length == 0) {
			mini.alert("没有待提交的日报！", "提示", null);
			return;
		}
		var title = "确定要提交吗？";
		if (flag) {
			title = "你的工作时间不足8小时，" + title;
		}
		mini.confirm(title, "确定？",
			function (action) {
				if (action == "ok") {
					mini.mask({
						el: document.body,
						cls: 'mini-mask-loading',
						html: '数据保存中...'
					});
					var ids = "";
					for (var i = 0; i < list.length; i++) {
						ids = ids + list[i].dayReportID + ",";
					}
					// debugger
					ids = ids.substr(0, ids.length - 1);
					var data = {
						"dayReportIDs": ids
					};
					http.post("/day/report/submitDayReport.do", data, function (page) {
						mini.unmask();
						if (page.success) {
							mini.alert("提交成功!", "提示", null);
							search();
						} else {
							mini.alert(page.message, "提示", null);
						}
					});
				}
			}
		);
	}

	function doCheck(type) {
		var rows = datagrid.getSelecteds();
		if (rows.length == 0) {
			mini.alert("请先选择要审批的记录", "提示", null);
			return;
		}
		var list = [];
		var flag = false;
		for (var i = 0; i < rows.length; i++) {
			// 状态为提交，工作时长或请假不为0，不是自己的日报记录
			if (rows[i].checkStatus == 1 && rows[i].userID != userID) {
				list.push(rows[i]);
			}
		}
		if (list.length == 0) {
			mini.alert("没有待审批的日报！", "提示", null);
			return;
		}
		var title = "确定要审批吗？";
		mini.confirm(title, "确定？",
			function (action) {
				if (action == "ok") {
					mini.mask({
						el: document.body,
						cls: 'mini-mask-loading',
						html: '数据保存中...'
					});
					var ids = "";
					for (var i = 0; i < list.length; i++) {
						ids = ids + list[i].dayReportID + ",";
					}
					// debugger
					ids = ids.substr(0, ids.length - 1);
					var checkStatus = 2;
					if (type == "reject") {
						checkStatus = 0;
					}
					var data = {
						"dayReportID": ids,
						"userID": userID,
						"checkStatus": checkStatus
					};
					http.post("/day/report/check.do", data, function (page) {
						mini.unmask();
						if (page.success) {
							mini.alert("提交成功!", "提示", null);
							search();
						} else {
							mini.alert(page.message, "提示", null);
						}
					});
				}
			}
		);
	}

	function onGridRenderer(e) {
		for (var i = 0, l = checkStatus.length; i < l; i++) {
			var g = checkStatus[i];
			if (g.id == e.value) return g.text;
		}
		return "";
	}

	function onWorkedRateRenderer(e) {
		var d = e.value;
		if (d > 0) return d * 100 + "%";
		else return 0;
	}

	function output() {
		mini.mask({
			el: document.body,
			cls: 'mini-mask-loading',
			html: '数据处理中...'
		});
		var data = queryForm.getData(true, false);
		var node = tree.getSelectedNode();
		if (node) {
			data["departID"] = node.departID;
			data["departID"] = "like";
			data["departName"] = node.departName;
		}
		// debugger
		// 如果角色是普通用户
		if (localUser.roleID == "normalUser") {
			data["condition"] = "userID='#userID#' or leaderID='#userID#'";
		}
		data["orderby"] = ["calendarDate,desc"];
		http.post("/day/report/outPut.do", data, function (page) {
			mini.unmask();
			if (page.success) {
				mini.alert("数据处理成功，准备下载!", "提示", null);
				var url = http.getUrl("/file/getFile.do");
				jQuery('<form action="' + url + '"method=post">' + // action请求路径及推送方法
					'<input type="text" name="url" value="' + page.dataMap.filename + '"/>' + // 文件路径
					'</form>')
					.appendTo('body').submit().remove();
			} else {
				mini.alert(page.message, "提示", null);
			}
		});
	}

	function createReport() {
		mini.mask({
			el: document.body,
			cls: 'mini-mask-loading',
			html: '数据处理中...'
		});
		var data = queryForm.getData(true, false);
		var params = {};
		params["date"] = data.calendarDate_from;
		params["userID"] = data.userID;
		debugger
		http.post("/user/createEmployeeCalendar.do", params, function (page) {
			mini.unmask();
			if (page.success) {
				mini.alert("日报生成完成!", "提示", null);
				datagrid.reload();
			} else {
				mini.alert(page.message, "提示", null);
			}
		});
	}
</script>

</html>